<?php 
session_start();
define("CCTRL", "tour");
define("CCTRLNAME", "tour");
class tour extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->load->model('admin-planners/tour_model','my_model');
            $this->load->model('admin-planners/setting_model','setting_model');
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
            include APPPATH . 'libraries/defu.php';
            $this->InitSetting();
        }
        function InitSetting(){
            $colModel=array(
                array(  "display"       =>"ID"                  ,"name"=>"ID"               ,"width"=>60        ,"sortable"=>true       ,"align"=>"center"      ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Company"             ,"name"=>"Type"             ,"width"=>120       ,"sortable"=>true       ,"align"=>"center"      ,"hide"=>false      ,"filter"=>true),
                array(  "display"       =>"Title"               ,"name"=>"Title"            ,"width"=>180       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>true ),
                array(  "display"       =>"Alias"               ,"name"=>"Alias"            ,"width"=>180       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false),
                array(  "display"       =>"UnitPrice"           ,"name"=>"UnitPrice"            ,"width"=>180       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false       ,"filter"=>false),
                array(  "display"       =>"Thumb"               ,"name"=>"Thumb"            ,"width"=>120       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Status"              ,"name"=>"Status"           ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Insert"              ,"name"=>"Insert"           ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Update"              ,"name"=>"Update"           ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false),
                array(  "display"       =>"Delete"              ,"name"=>"Delete"           ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false)
            );
            if(isset($_SESSION["ADP"]["USER"])){
                if(isset($_SESSION["ADP"][CCTRL]["colModel"])){
                    $colModel= $_SESSION["ADP"][CCTRL]["colModel"];
                }else{
                    $_SESSION["ADP"][CCTRL]["colModel"]=$colModel;
                }
            }
            if(!isset($_SESSION["ADP"][CCTRL]["tourtype"])){
                $data=$this->setting_model->getByKey("tourtype");
                if(isset($data[0])){
                    $_SESSION["ADP"][CCTRL]["tourtype"]   =  objectToArray(@json_decode($data[0]->Value));
                }else{
                    $_SESSION["ADP"][CCTRL]["tourtype"] =array();
                }
            }
        }
        function type(){
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("admin-planners/01_login");
                return;
            }
            $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->display("admin-planners/tour/tourtype");
            
        }
        function index(){
            if(isset($_SESSION["ADP"]["Filter_".CCTRL]))unset($_SESSION["ADP"]["Filter_".CCTRL]);
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("sys/04_access_denied");
                return;
            }
            
            $companys=$_SESSION["ADP"][CCTRL]["tourtype"];
            foreach ($companys as $k=>$v){
                $Data["filter_value"][]=array("value"=>$v["Name"],"display"=>$v["Name"]);
            }
            $Data["flexigrid_settings"]["colModel"]=$_SESSION["ADP"][CCTRL]["colModel"];
            $Data["flexigrid_settings"]["display"]=$_SESSION["ADP"][CCTRL]["display"];
            $Data["flexigrid_settings"]["filterModel"]=filterModel($_SESSION["ADP"][CCTRL]["colModel"]);
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("admin-planners/".CCTRLNAME."/".CCTRLNAME);
        }
        function addtourtype(){
            $Name=$_POST["Name"];
            $Des=$_POST["Des"];
            if( (!isset($_POST["Name"])) || $_POST["Name"]==""){
                $msgs[]="CompanyName does not empty.";
            }
            $tourtype=$_SESSION["ADP"][CCTRL]["tourtype"];
            $tourtype[convertUrl($Name)]=array("Name"=>$Name,"Des"=>$Des);
            $code=1;
            $Params=array(
                "Key"=>"tourtype",
                "Name"=>"Tour Type",
                "Type"=>"settings",
                "Value"=>json_encode($tourtype)
            );
            if(strlen(json_encode($tourtype))>=2000){
                $code=-1;
                $msg="Max legnth of conpany.";
            }elseif($this->setting_model->insert_onduplicate_update("tourtype",$Params)){
                $_SESSION["ADP"][CCTRL]["tourtype"]=$tourtype;
                $msg="Tour type have been change.";
            }    
            
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function deltourtype(){
            $Name=$_POST["Name"];
            $tourtype=$_SESSION["ADP"][CCTRL]["tourtype"];
            unset($tourtype[convertUrl($Name)]);
            $code=1;
            $Params=array(
                "Key"=>"tourtype",
                "Name"=>"Tour Type",
                "Type"=>"settings",
                "Value"=>json_encode($tourtype)
            );
            if($this->setting_model->insert_onduplicate_update("tourtype",$Params)){
                $_SESSION["ADP"][CCTRL]["tourtype"]=$tourtype;
            }
            $msg="Tour type have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function Edit(){
            $Data=null;
            if(isset($_POST["ID"])){
                $obj=$this->my_model->get($_POST["ID"]);
                if(count($obj)>0){
                    $Data["OBJ"]=  $obj[0];
                }
            }
            $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display('admin-planners/tour/02_edit');
        }
        public function ChangeColumnDisplay(){ 
            for($i=0;$i<count($_SESSION["ADP"][CCTRL]["colModel"]);$i++){
                if($_SESSION["ADP"][CCTRL]["colModel"][$i]["name"]==$_POST["col"]){
                    $_SESSION["ADP"][CCTRL]["colModel"][$i]["hide"]=($_POST["hide"]==1?true:false);
                    break;
                }
            }
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function ChangeDeleteDisplay(){
            $_SESSION["ADP"][CCTRL]["display"]=$_POST["showDelete"];
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function ChangeStatus(){
            
            if(isset($_POST["Status"]) && isset($_POST["ID"])){
                $Params=array(
                    "Status"=>$_POST["Status"]
                );
                    if($this->my_model->update($_POST["ID"],$Params)){
                        $code=1;
                        $msg="Status' have been changed.";
                    }else{
                        $code=-1;
                        $msg="Fail. Cant change Status of this record";
                    }
            }else{  
                $code=-1;
                $msg="Fail.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function Delete(){
            
            if(isset($_POST["ID"])){
                if($this->my_model->delete($_POST["ID"])){
                    $code=1;
                    $msg="Record have been deleted.";
                }else{
                    $code=-1;
                    $msg="Fail. Cant delete this Record";
                }
            }else{  
                $code=-1;
                $msg="Fail.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function Restore(){
            
            if(isset($_POST["ID"])){
                if($this->my_model->retore($_POST["ID"])){
                    $code=1;
                    $msg="Record have been retored.";
                }else{
                    $code=-1;
                    $msg="Fail. Cant retore this Record";
                }
            }else{  
                $code=-1;
                $msg="Fail.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function Save(){
            
            $vlows=array("\\\"","\\'");
            $vals=array("\"","'");
            $msgs=array();
            
            if( (!isset($_POST["Title"])) || $_POST["Title"]==""){
                $msgs[]="Title does not empty.";
            }
            if( (!isset($_REQUEST["Content"])) || $_REQUEST["Content"]==""){
                $msgs[]="Content does not empty.";
            }
            if( (!isset($_POST["UnitPrice"])) || $_POST["UnitPrice"]=="" || !is_numeric($_POST["UnitPrice"])){
                $msgs[]="UnitPrice does not empty and UnitPrice must be numeric.";
            }
//            if( (isset($_REQUEST["PriceList"])) && strlen($_REQUEST["PriceList"])>2000){
//                $msgs[]="PriceList limit of 2000 characters.".strlen($_REQUEST["PriceList"]);
//            }
//            if( (isset($_REQUEST["Conditions"])) && strlen($_REQUEST["Conditions"])>2000){
//                $msgs[]="Conditions limit of 2000 characters.";
//            }
            //echo $_Params["Content"];return;
            if(count($msgs)>0){
                $code=-44;
                $msg="";
                foreach ($msgs as $m){
                    $msg.="$m<br/>";
                }
            }else{
                $Params=array(
                    "Alias"=>  convertUrl($_POST["Title"]),
                    "Title"=>$_POST["Title"],
                    "Thumb"=>$_POST["Thumb"],
                    "UnitPrice"=>$_POST["UnitPrice"],
                    "Type"=>$_POST["Type"],
                    "Content"=>str_replace($vlows, $vals,$_REQUEST["Content"]),
                    "PriceList"=>str_replace($vlows, $vals,$_REQUEST["PriceList"]),
                    "Conditions"=>str_replace($vlows, $vals,$_REQUEST["Conditions"])
                    
                );
                if(isset($_POST["ID"]) && $_POST["ID"]!=""){
                    if($this->my_model->update($_POST["ID"],$Params)){
                        $code=1;
                        $msg="Success. Record have been updated.";
                    }else{
                        $code=-1;
                        $msg="Fail. Cant update this Record.";
                    }
                }else{
                    if($this->my_model->insert($Params)){
                        $code=1;
                        $msg="Success. Record have been added to database.";
                    }else{
                        $code=-1;
                        $msg="Fail. Cant insert Record to database.";
                    }
                }
                //echo"<pre>";print_r($VideoParams);echo"</pre>";return;
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function FlexiGridData(){
            
            $data=$this->my_model->FlexiGridData();
            header("Content-type: application/json");
            $jsonData = array('page'=>$data["page"],'total'=>$data["total_rows"],'rows'=>array());
            foreach($data["rows"] AS $row){
                    //If cell's elements have named keys, they must match column names
                    //Only cell's with named keys and matching columns are order independent.
                    $cell=array();
                    foreach ($_SESSION["ADP"][CCTRL]["colModel"] as $col){
                        $c=$col["name"];
                        $cell[$col["name"]]=$row->$c;
                        
                    }
                    $cell['Type']=htmlentities_UTF8($row->Type);
                    $cell['Title']=htmlentities_UTF8($row->Title);
                    $id="ID";
                    $entry = array('id'=>$row->$id,
                            'cell'=>$cell
                    );
                    $jsonData['rows'][] = $entry;
            }
            echo json_encode($jsonData);
        }
        function Filter(){
            if($_POST["type"]=="me")
                $_SESSION["ADP"]["Filter_".CCTRL]["Filters"]=$_POST["filters"];
            $_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=$_POST["type"];
            echo json_encode(array("code"=>1,"msg"=>"Filter susess."));
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */